define(function(require, exports, module) {
  var $ = require("$");
  var bootbox = require("bootbox");
  var dtUtil = require('datatableutil');
  require("bootstrap_commonrequire");
  require("moment");
  require('daterangepicker.css');
  require('bootstrap-daterangepicker');
  require('select2.css');
  require('select2');

  exports.init = function(param) {

    $(document).ready(function() {

      // 加载自定义模板-条件区域
      var template = require("./appinfo.tpl");
      var Widget = require("widget");
      var Component = Widget.extend({
        template : template
      });
      new Component({
        renderTo : "queryAreaId"
      });

      // 绑定isv名称下拉列表
      $.ajax({
        type : "POST",
        dataType : "json",
        url : "/user/queryIsv.do",//GLOBAL.URL.ADMIN_GETCARRIEROPTIONS,// wp.huang
        async : false,
        success : function(data) {
          $("#isvid").select2({
            placeholder : "ISV名称",
            allowClear : true,
            data : data
          });
        },
        error : function(data) {
          bootbox.alertTimeout('初始化ISV名称数据失败！');
        }
      });

      // 初始化日期范围选择控件
      var DateRange = require('daterangeinput');
      var daterangeAttrs = {
        daterangeId : "querydatewrapper_rang",
        label : "日期"
      };
      var daterangeCom = new DateRange({
        attrs : daterangeAttrs,
        renderTo : 'querydatewrapper'
      });

      function getCondition() {
        //获取查询条件
        var isvid = $('#isvid').val();
        var queryStr = "";

        if (isvid != '' && isvid != null) {
          queryStr += ("isv_id=" + isvid + ";");
        }

       
        return queryStr;
      }
      //加载表单模板和数据
      var Table = require("table");
      // 查询内容显示
      new Table({
        attrs : {
          id : "data_table",
          isHovered : false,
          isStriped : true,
          isBordered : false
        },
        renderTo : param.divWrapperId

      });
      // isv信息显示
      var isvinfo_data_table = $('#data_table').dataTable({
        "sDom" : "<'hidden'<'span6'l><'hidden'f>r>t<'row-fluid'<'span6'i><'span6'p>>",
        "bJQueryUI" : false,
        "oLanguage" : {
          "sUrl" : "/business/js/common/table/src/lang/dataTable.cn.txt"
        },
        "sPaginationType" : "bootstrap", // 分页自带两种样式，full_numbers和two_button(默认)
        'bPaginate' : true, // 是否分页
        "bProcessing" : true, // 当datatable获取数据时候是否显示正在处理提示信息。
        "bServerSide" : true, // 指定服务器端分页
        "bDeferRender" : true,
        "bFilter" : true,
        "bSort" : true,
        "bInfo" : true,
        "sAjaxSource" : param.ajaxUrl,
        "iDisplayLength" : DATATABLES_CONFIG.iDisplayLength.DEFAULT, // 默认分页的每页记录数
        "fnServerData" : function(sSource, aoData, fnCallback, oSettings) {
          var queryparam = dtUtil.convert_aoData(aoData);

          //拼接where查询条件
          var cond = getCondition();
          if (cond != '' && cond != null) {
            queryparam.q += cond;
          }

          // 添加自己的额外参数
          oSettings.jqXHR = $.ajax({
            "dataType" : 'json',
            "contentType" : "application/json;charset=UTF-8",
            "type" : "GET",
            "url" : sSource,
            "data" : queryparam, // 将请求数据转换为后台要求的JSON格式
            "success" : function(json) {
              if (json.flag == '000000') {
                dtUtil.successCallback(json, aoData, fnCallback);
              } else {
                oauth2Relogin();
              }
            }
          });
        },
        "aoColumns" : [ {
          "sTitle" : "id",
          "bSortable" : false,
          "mData" : "id",
          "bVisible" : false
        }, {
          "sTitle" : "应用名称",
          "bSortable" : false,
          "mData" : "name"
        },
        {
          "sTitle" : "ISV",
          "bSortable" : false,
          "mData" : "isvname"
        }, {
          "sTitle" : "标识",
          "bSortable" : false,
          "mData" : "identify"
        },
        {
          "sTitle" : "备注",
          "bSortable" : false,
          "mData" : "note",
          "bVisible" : true
        },
        {
          "sTitle" : "操作",
          "bSortable" : false,
          "mData" : "status",
          "mRender" : function(data, status, full) {
            var ret = '<div class="btn-group" style="margin-top: 8px;">';
            ret += '<a href="javascript:void(0);" class="btn icon-reply contrast" title="修改"></a>';
            return ret += '</div>';
          }
        } ],
        "fnDrawCallback" : function(oSettings) {
          // 表格绘制完成后，去除width属性，使表格可缩放
          $('#' + oSettings.sTableId).css("width", "");
        }
      });

      //新建ISV按钮
      $("#creatbtn").on("click", function() {
        $("#appForm").hide();
        $("#queryAreaId").hide();
        $("#appinfo_data_table").hide();
        var appForm = require("./appinfoform");
        appForm.init("appinfo_edit_table", null);
        $('#appinfo_edit_table').show();
      });
      //修改按钮
      $("#data_table tbody").on("click", "tr>td>div>a.icon-reply", function(e) {
        $("#appForm").hide();
        $("#queryAreaId").hide();
        $("#appinfo_data_table").hide();
        var rowNode = this.parentNode.parentNode.parentNode;
        var rowData = isvinfo_data_table.fnGetData(rowNode);
        var isvinforeturnform = require("./appinforeturnform");
        isvinforeturnform.init("appinfo_edit_table", rowData);
        $('#appinfo_edit_table').show();
      });
      //查询按钮
      $('#querybtn').click(function() {
        isvinfo_data_table.fnFilter("");
        return false;
      });

      //重置按钮
      $('#restbtn').click(function() {
        $('#isvid').select2("val", "");
        daterangeCom.clearInput();
        isvinfo_data_table.fnFilter("");
        return false;
      });

    });

  };
});